package com.kokomi.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kokomi.pojo.Book;
import com.kokomi.pojo.MyPageData;
import com.kokomi.pojo.RedisConst;
import com.kokomi.pojo.Result;
import com.kokomi.pojo.VO.BookTypeVO;
import com.kokomi.pojo.VO.BookVO;
import com.kokomi.service.BookService;
import com.kokomi.service.BookTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Component  //放到Spring中容器
@Slf4j
public class BookMallTask {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    BookService bookService;
    @Autowired
    BookTypeService bookTypeService;

    @Scheduled(cron = "0 0 * * * 1-5")
    public void bookCacheTask() {
        //1.删除redis中key为book:list的缓存
        deleteKeys(RedisConst.BOOK_LIST+"*");
        //2.删除redis中key为book:type:list的缓存
        deleteKeys(RedisConst.BOOK_TYPE_LIST+"*");

        //3.查询所有图书类型，并将图书类型缓存到redis中

        List<BookTypeVO> bookTypes = new ArrayList<>();

        // 获取图书id和名字
        bookTypeService.list().forEach(book -> {
            //新建一个对象
            BookTypeVO bookType = new BookTypeVO();
            bookType.setId(book.getId());
            bookType.setName(book.getName());
            bookTypes.add(bookType);
        });
        redisTemplate.opsForValue().set("book:list::com.kokomi.controller.BookController:getBookByTypeId:",
                Result.ok(bookTypes,"获取图书类型成功",200));

        //4.查询所有图书类型下的图书第一页存入缓存，注意KEY要保持一致
        bookTypes.forEach(bookType -> {
            Page<BookVO> page = bookService.getBookByTyId(bookType.getId(),1,5);
            MyPageData myPageData = new MyPageData(page.getTotal(), page.getRecords());
            redisTemplate.opsForValue().set("book:list::com.kokomi.controller.BookController:getBookByTypeId:"+bookType.getId()+":1:5",
                    Result.ok(myPageData,"获取图书成功",200));
            log.info("缓存图书类型下的图书第一页成功");
        });
        //5.查询热销图书下的图书第一页存入缓存，注意KEY要保持一致
        Page<Book> page = new Page<>(1,5);
        Page<Book> buyCount = bookService.page(page, new QueryWrapper<Book>().orderByDesc("buy_count"));
        MyPageData myPageData = new MyPageData();
        myPageData.setTotal(buyCount.getTotal());
        myPageData.setData(buyCount.getRecords());
        redisTemplate.opsForValue().set("book:list::com.kokomi.controller.BookController:getBookBySales:1:5",
                Result.ok(myPageData,"获取图书成功",200));
        log.info("缓存热销图书下的图书第一页成功");

    }

    private void  deleteKeys(String pattern){
        Set<String> keys = redisTemplate.keys(pattern);
        keys.forEach(key -> redisTemplate.delete(key));
        log.info("删除redis中key为{}的缓存",pattern);
    }
}
